<?php
/**
 * Clase DBManager
 * @author chalodrv@yahoo.es
 */
class DBManager
{
	/**
	 * Variables
	 */
	var $dbLink;
	var $insertId;
	var $errorCode;
	var $errorDescription;

	/**
	 * Constructor
	 */
	function __construct()
	{
		$this->dbHost = DB_HOST;
		$this->dbUser = DB_USER;
		$this->dbPass = DB_PASS;
		$this->dbName = DB_NAME;
	}

	/**
	 * Método connect
	 * @param string $dbHost
	 * @param string $dbUser
	 * @param string $dbPass
	 * @param string $dbName
	 */
	function connect()
	{
		if (!$this->dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass)) {
			return false;
		}
		mysql_select_db($this->dbName, $this->dbLink);
		return true;
	}

	/**
	 * Método runQuery
	 * @param string $query
	 */
	function runQuery($query)
	{
		// Vaciamos el contenido de las variables de error
		$this->insertId = "";
		$this->errorCode = "";
		$this->errorDescription = "";

		// Ejecutamos la query
		$res = mysql_query($query, $this->dbLink);

		// Si la query falla, guardamos el error en las variables de error
		if (!$res) {
			$this->errorCode = mysql_errno();
			$this->errorDescription = mysql_error();
		} else {
			$this->insertId = mysql_insert_id();
		}

		// Devolvemos el resultado de la ejecución
		return $res;
	}

	/**
	 * Método getRow
	 * @param mysqlresource $res
	 */
	function getRow(&$res)
	{
		return mysql_fetch_assoc($res);
	}

	/**
	 * Método getRowCount
	 * @param mysqlresource $res
	 */
	function getRowCount(&$res)
	{
		return mysql_num_rows($res);
	}

	/**
	 * Método freeResult
	 * @param mysqlresource $res
	 */
	function freeResult(&$res)
	{
		mysql_free_result($res);
	}

	/**
	 * Método escapeString
	 * @param string $string
	 */
	function escapeString($string)
	{
		return mysql_real_escape_string($string);
	}

	/**
	 * Método getInsertId
	 */
	function getInsertId()
	{
		return $this->insertId;
	}

	/**
	 * Método getErrorCode
	 */
	function getErrorCode()
	{
		return $this->errorCode;
	}

	/**
	 * Método getErrorDescription
	 */
	function getErrorDescription()
	{
		return $this->errorDescription;
	}

	/**
	 * Método getLanguages
	 */
	function getLanguages()
	{
		$query = "SELECT id, code, isDefault FROM languages ORDER BY 1";
		if (!$res1 = $this->runQuery($query)) {
			return "Error de MySQL: ".$this->getErrorDescription();
		} else {
			if (!$this->getRowCount($res1)) {
				return "No hay datos que mostrar.";
			} else {
				$languages = array();
				while ($row1 = $this->getRow($res1)) {
					$languages[] = $row1;
				}
				return $languages;
			}
		}
	}

	/**
	 * Método getData
	 * @param string $table
	 */
	function getData($table=null, $sql=null)
	{
		if (!is_null($sql)) {
			$query = $sql;
		} else {
			switch ($table) {
				case "content":
					$query = "SELECT c.id, c.html, s.name AS section
							  FROM content c
							  JOIN sections s ON c.sectionId = s.id
							  ORDER BY c.id";
					break;
				case "links":
					$query = "SELECT id, title, url, description, position
							  FROM links
							  ORDER BY position";
					break;
				case "blogEntries":
					$query = "SELECT id, visible, alias, title, introduction, description, created, createdBy
							  FROM blogEntries
							  ORDER BY created DESC";
					break;
				case "faq":
					$query = "SELECT id, question, answer, position
							  FROM faq
							  ORDER BY position";
					break;
				case "news":
					$query = "SELECT id, visible, alias, title, introduction, description, created, createdBy
							  FROM news
							  ORDER BY created DESC";
					break;
				default:
					return "Opción incorrecta.";
			}
		}

		if (!$res1 = $this->runQuery($query)) {
			return "Error de MySQL: ".$this->getErrorDescription();
		} else {
			if (!$this->getRowCount($res1)) {
				return "No hay datos que mostrar.";
			} else {
				$data = array();
				while ($row1 = $this->getRow($res1)) {
					foreach ($row1 as $key=>$value) {
						$row1[$key] = utf8_encode($value);
					}
					$data[] = $row1;
				}
				return $data;
			}
		}
	}

	/**
	 * Método getSections
	 */
	function getSections()
	{
		$query = "SELECT id, alias, level, parentId, name
				  FROM sections
				  WHERE level = 1
				  ORDER BY id";
		if (!$res1 = $this->runQuery($query)) {
			return "Error de MySQL: ".$this->getErrorDescription();
		} else {
			if (!$this->getRowCount($res1)) {
				return "No hay datos que mostrar.";
			} else {
				$sections = array();
				while ($row1 = $this->getRow($res1)) {
					$sectionId = $row1["id"];
					$sections[] = array(
						"id" => $sectionId,
						"alias" => $row1["alias"],
						"level" => $row1["level"],
						"parentId" => $row1["parentId"],
						"name" => utf8_encode($row1["name"])
					);
					// Subsections
					$query = "SELECT id, alias, level, parentId, name
							  FROM sections
							  WHERE level = 2
							  AND parentId = '$sectionId'
							  ORDER BY id";
					if ($res2 = $this->runQuery($query)) {
						if ($this->getRowCount($res2)) {
							while ($row2 = $this->getRow($res2)) {
								$sections[] = array(
									"id" => $row2["id"],
									"alias" => $row2["alias"],
									"level" => $row2["level"],
									"parentId" => $row2["parentId"],
									"name" => utf8_encode($row2["name"])
								);
							}
						}
					}
				}
			}
			return $sections;
		}
	}
}
?>
